require(['init'], function () {
  if(!localStorage.lang){
    localStorage.setItem('lang','zh');
  }
  if(localStorage.lang && localStorage.lang=='zh'){
    $('body').append('<script src="codebase/locale/locale_cn.js"  type="text/javascript" charset="utf-8"></script>');
  }
  else {
    $('body').append('<script src="codebase/locale/locale_en.js"  type="text/javascript" charset="utf-8"></script>');
  }
  function getCourseType() {
    ajaxMethod({
      url: '/api/coursetypes',
      success: function (data) {
        loadSchoolLessonTime(data)  
      }
    })
  }

  function loadSchoolLessonTime(data) {
    var courseList = [
      {
        key: 0,
        label:i18next.t('table.pleasecourse')//请选择课程类型
      }
    ];

    $.each(data, function (i, v) {
      courseList.push({
        key: v.coursetypeuid,
        label: v.coursetypename,
        color: v.colorvalue
      })
    });

    var lightbox = [
      {
        name: i18next.t('table.totalquota'),//总名额
        type: "template",
        map_to: "quotas",
        height: 30
      },
      {
        name: i18next.t('table.quotaused'),//已用名额
        type: "template",
        map_to: "usequotas",
        height: 30
      },
      {
        name:i18next.t('course.coursetype'),//课程类型
        type: "template",
        map_to: "coursetype",
        // options: courseList,
        default_value: '0',
        height: 30
      },
      {
        name: i18next.t('table.lessonsname'),//课节名称
        type: "template",
        map_to: "lessonname",
        height: 30,
        default_value: ''
      },
      {
        name:  i18next.t('table.classresources'),//课节资源
        type: "template",
        map_to: "foldername"
      },
      {
        name:i18next.t('info.studentlist'),//学生列表
        type: "template",
        map_to: "students",
        height: 30
      },
      {
        name: i18next.t('table.auditor'),//旁听生
        type: "template",
        map_to: "auditors"
      },
      {
        name: "time",
        type: "template",
        map_to: "custom_time",
        height: 30
      }
    ]

    schedulerInit({
      url: '/api/schedule',
      schedulerType: 3,
      courseList: courseList,
      create_lightbox: lightbox,
      drag_move: false,
      buttons_left: [],
      buttons_right: ['dhx_cancel_btn'],
      icons_select: ['icon_details'],
      processData: function (data, courseList) {
        var dataArr = [];
        $.each(data, function (i, v) {

          var studentArr = v.students || [];
          var studentNameArr = [];
          $.each(studentArr, function () {
            studentNameArr.push(this.name);
          });

          //旁听
          var auditArr = v.auditors || [];
          var auditNameArr = [];
          $.each(auditArr, function () {
            auditNameArr.push(this.name);
          });

          var cur = {
            start_date: new Date(formatTime(v.begintime)),
            end_date: new Date(formatTime(v.endtime)),
            text: v.teacherName,

            quotas: v.quotas || '0',
            usequotas: v.usequotas || '0',
            students: studentNameArr.join(', ') || i18next.t('table.empty'),
            teachermobile: v.teachermobile,
            teachername: v.teachername,
            color: getCourseColorById(courseList, v.coursetypeid) || '#96d6e3',
            lessonname: v.name,
            coursetype: v.coursetypename,
            foldername: v.foldername,
            auditors: auditNameArr.join(', ') || i18next.t('table.empty'),
            custom_time:v.begintime+"  "+v.endtime
          }


          if (cur.quotas != '0' && cur.quotas === cur.usequotas) {
            cur.color = 'red';
          }

          dataArr.push(cur);
        })
        return dataArr;
      }
    })
  }

  getCourseType();
})
